<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            box-sizing: border-box;
            -webkit-tap-highlight-color: transparent;
            -webkit-touch-callout: none;
            -moz-user-select: none;
            -o-user-select: none;
            -khtml-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
            -moz-user-select: -moz-none;
            -moz-user-select: none;
            user-select: none;
        }

        html {
            background: #eee;
        }

        .nav {
            width: 100%;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 999;
            background: #fff;
        }

        .nav .nav-list {
            margin: 0 auto;
            max-width: 600Px;
            display: flex;
            justify-content: space-around;
            align-items: center;
            height: 44px;
        }

        .nav .nav-list>div {
            font-size: 16px;
            font-family: PingFangSC-Regular, PingFang SC, sans-serif;
            font-weight: 400;
            color: #666;
            line-height: 16px;
            cursor: pointer;
            text-align: center;
            background-image: linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;

        }

        .nav .nav-list .nav-list--active {
            font-family: PingFangSC-Semibold, PingFang SC, sans-serif;
            font-weight: bold;
            color: #333333;
            transition: all 0.3s;
            background-color: #FFE53B;
            background-image: linear-gradient(147deg, #FFE53B 0%, #FF2525 74%);
        }

        .nav-line {
            margin: 0 auto;
        }

        .nav-line>div {
            width: 18px;
            height: 4px;
            background-color: #4158D0;
            background-image: linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%);
            border-radius: 2px;
            transition-duration: 0.3s;
            position: relative;
            top: -7px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="nav">
            <!-- 标题列表 -->
            <div class="nav-list">
                <div v-for="(item, index) in navList" :key="item" @click="changeTitle(index)"
                    :class="{ 'nav-list--active': index === active }">
                    {{ item }}
                </div>
            </div>
            <!-- 下划线 -->
            <div class="nav-line">
                <div :style="{transform: 'translateX(' + translateX + 'px) translateX(-50%)',}"></div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                navList: ['首页', '推荐', '关注', '会员', '消息'],
                active: 0,
                translateX: 0
            },
            methods: {
                /** 
                 * 点击切换标题
                 * @param {number} index 菜单序列(点击的第index个)
                */
                changeTitle(index) {
                    this.active = index;
                    this.setUnderLine();
                },
                /**
                 * 设置下划线
                */
                setUnderLine() {
                    // 屏幕宽
                    let width = document.documentElement.clientWidth;
                    // 每个菜单所占宽度 = 屏宽 / 菜单个数
                    let slidingWidth = (width / this.navList.length).toFixed(2);
                    // 半个菜单宽度 （为了将下划线与菜单对齐）
                    let halfSlidingWidth = (slidingWidth / 2).toFixed(2);
                    // 水平位移目的坐标 = 半个菜单宽度 + (菜单宽 x 当前所在的index)
                    this.translateX = Number(halfSlidingWidth) + Number(slidingWidth * this.active);
                }
            },
            mounted() {
                this.setUnderLine();
            }
        })
    </script>
</body>

</html>